/*  --------------------------- (C) COPYRIGHT 2020 Fortiortech ShenZhen -----------------------------
    File Name      : Interrupt.c
    Author         : Fortiortech  Appliction Team
    Version        : V1.0
    Date           : 2020-04-10
    Description    : This file contains .C file function used for Motor Control.
    ----------------------------------------------------------------------------------------------------
                                       All Rights Reserved
    ------------------------------------------------------------------------------------------------- */
#include "MyProject.h"

void motoronoff(void);
//uint16 xdata spidebug[4] = {0};
/*  -------------------------------------------------------------------------------------------------
    Function Name  : LVW_TSD_INT
    Description    : LVW interrupt，芯片低压预警中断
    Date           : 2020-04-10
    Parameter      : None
    ------------------------------------------------------------------------------------------------- */
void LVW_TSD_INT(void) interrupt 0 // LVW & TSD interrupt
{
    if (ReadBit(LVSR, LVWIF))
    {
        if (ReadBit(LVSR, LVWF))
        {
            ClrBit(LVSR, LVWF);
        }
        
        ClrBit(LVSR, LVWIF);
    }
    
    if (TSDIF)
    {
        mcState = mcFault;    
        mcFaultSource = FaultTSD;     //TSD过温故障
        FaultProcess();               //错误处理函数
		    TSDIF = 0;
    }
}

/*  -------------------------------------------------------------------------------------------------
    Function Name  : PO0_INT
    Description    : PO0_INT外部中断0，
    Date           : 2020-04-10
    Parameter      : None
    ------------------------------------------------------------------------------------------------- */
void PO0_INT(void) interrupt 1 /* LIN同步信号检测*/
{
    uint16 time = 0;
    
    if (IF0)
    {
        if (GP01 == 0) /*接收到下降沿*/
        {
            if (Lin.RUN_flag == 0)
            {
                Lin.RUN_flag = 1;
                ClrBit(TIM3_CR1, T3EN); /*先停止计数*/
                TIM3__CNTR = 1;         /*清除time3的计数器*/
                Lin.Timeblank = 0;
                SetBit(TIM3_CR1, T3EN); /*使能计数*/
            }
        }
        else
        {
            if (Lin.RUN_flag == 1)
            {
                /*低电平持续时间满足13位的同步间隔断要求*/
                ClrBit(TIM3_CR1, T3EN); /*先停止计数*/
                Lin.Timeblank = TIM3__CNTR;
                
                /*19200对应的同步时间 13位  0.677MS  2031计数值  同步阶段误差5%可以接收*/
                if ((Lin.Timeblank < 2230) && (Lin.Timeblank > 1930))
                {
                    Lin.RUN_flag = 11;
                    MODE_UART(); /*切换到串口模式*/
                }
                else
                {
                    Lin.RUN_flag = 0;
                }
                
                /*数据超时错误使用防止无法出串口中断 */
                /*数一组数据最大不会停留7ms 根据发送调度表计算*/
                TIM3__CNTR = 1;         /*清除time3的计数器*/
                SetBit(TIM3_CR1, T3EN); /*使能计数*/
            }
        }
        
        IF0 = 0; /* clear P01 interrupt flag*/
    }
}
/*  -------------------------------------------------------------------------------------------------
    Function Name  : EXTERN_INT
    Description    : 睡眠唤醒
    Date           : 2020-04-10
    Parameter      : None
    ------------------------------------------------------------------------------------------------- */
void EXTERN_INT(void) interrupt 2
{
}

void DRV_ISR(void) interrupt 3
{
    static uint8 Section = 0;
    
    if (ReadBit(DRV_SR, DCIF)) /* 比较中断*/
    {          
		 			
        /**************计换相次数 记录位置移动圈数************************/
        if ((mcState == mcRun) || (mcState == mcStart))
        { 
					  BL_S_Function(); /*启动函数*/
						if((Section != TIM1_CR4)&&((TIM1_CR4 == 1)||(TIM1_CR4 == 4)))
						{
							GP12=~GP12;
						}
						Section = TIM1_CR4;
        }
				

				Duty_Function();      /* Duty调整函数   速度闭环计算*/
				
//         #if defined (SPI_DBG_SW)            //软件调试模式
//        {
//            spidebug[0] = TIM1__RCNTR;
//            spidebug[1] = 0;
//            spidebug[2] = 0;
//            spidebug[3] = 0;
//        }
//        #endif

				DRV_SR = (DRV_SR | SYSTIF) & ~(DCIF);                            // 清零DCIF中断标志位 

    }
}

/* TIM1中断 */
void TIM1_INT(void) interrupt 5
{
    if (ReadBit(TIM1_SR, T1PDIF)) //位置检测中断标志位
    {
        ClrBit(TIM1_SR, T1PDIF);
    }
}

/*---------------------------------------------------------------------------*/
/*  Name     :   void TIM3_INT(void) interrupt 9
    /* Input    :   NO
    /* Output   :   NO
    /* Description: LIN总线计时使用
/*---------------------------------------------------------------------------*/
void TIM3_INT(void) interrupt 9
{
    if (ReadBit(TIM3_CR1, T3IP)) /*比较中断事件*/
    {
        ClrBit(TIM3_CR1, T3IP);
    }
    
    if (ReadBit(TIM3_CR1, T3IR)) /*比较中断事件*/
    {
        ClrBit(TIM3_CR1, T3IR);
    }
    
    if (ReadBit(TIM3_CR1, T3IF)) /*溢出中断事件*/
    {
        if (Lin.RUN_flag == 1)
        {
            Lin.RUN_flag = 0; /*同步间隔段超时错误*/
            Lin.Err = ERR_BLANK;
        }
        else if (Lin.RUN_flag == 11)
        {
            Lin.RUN_flag = 0; /*同步信号超时错误*/
        }
        else if (Lin.RUN_flag == 12)
        {
            Lin.RUN_flag = 0; /*接收ID信号超时错误*/
            Lin.Err = ERR_PID;
        }
        else if (Lin.RUN_flag == 13)
        {
            Lin.RUN_flag = 0; /*接收数据超时错误*/
            Lin.Err = ERR_DATA;
        }
        else if (Lin.RUN_flag == 23)
        {
            Lin.RUN_flag = 0; /*发送数据超时错误*/
        }
        
        Cutover_IO; /*切换回io模式*/
        /* put_char(0X55);//测试串口*/
        ClrBit(TIM3_CR1, T3IF);
    }
}
/*  -------------------------------------------------------------------------------------------------
    Function Name  : SYStick_INT
    Description    : 1ms定时器中断（SYS TICK中断），用于处理附加功能，如控制环路响应、各种保护等。中断优先级低于FO中断和FOC中断。
    Date           : 2020-04-10
    Parameter      : None
    ---------------------------  ---------------------------------------------------------------------- */

uint16 test_DCbus;
void SYStick_INT(void) interrupt 10
{
    // SYS TICK中断
    if (ReadBit(DRV_SR, SYSTIF))
    {
        SetBit(ADC_CR, ADCBSY);
        /*ADC数据读取*/
        LPF_MDU(ADC14_DR, 40, mcFocCtrl.mcDcbusFlt, mcFocCtrl.mcDcbusFlt_LSB); /*母线电压读取，过欠压保护使用 */
        mcFocCtrl.mcIbusADC = ADC2_DR; /* RC滤波后的母线电流读取*/
        
        if (mcFocCtrl.mcIbusADC > mcCurOffset.Iw_busOffset) /*减去偏置电压*/
        {
            mcFocCtrl.mcIbusADC = mcFocCtrl.mcIbusADC - mcCurOffset.Iw_busOffset;
        }
        else
        {
            mcFocCtrl.mcIbusADC = 0;
        }
        
        LPF_MDU(mcFocCtrl.mcIbusADC, 63, mcFocCtrl.mcIbusFlt, mcFocCtrl.mcIbusFlt_LSB); /*对电流进行滤波*/
        LPF_MDU(ADC7_DR, 100, mcFocCtrl.mcNtcTempFlt, mcFocCtrl.mcNtcTempFlt_LSB); /*读取NTC电压 */
        /*电机保护函数*/
        Fault_Detection(); /*故障检测和保护*/
								
        /*计数值操作  1ms计数 */
        if (ONOFFTest.PowerOFF_Count)
        { ONOFFTest.PowerOFF_Count--; }
												
        if (mcFocCtrl.State_Count)
        { mcFocCtrl.State_Count--; }               
              
        /*延迟换相角度+续流屏蔽角度 调整*/
        StarRampDealwith();
				
				DRV_SR = (DRV_SR | DCIF) & ~(SYSTIF);           // 清零标志位
		
    }
}

/*  -------------------------------------------------------------------------------------------------
    Function Name  : TIM4_INT
    Description    : TIM4主要用于150度方波的插入
    Date           : 2021-03-30
    Parameter      : None
    ------------------------------------------------------------------------------------------------- */
void TIM4_INT(void) interrupt 11
{
    uint8 TIM1_CR4_150;
    
    if (ReadBit(TIM4_CR1, T4IF))
    {      
        ClrBit(TIM4_CR1, T4EN); //关闭TIM4
        ClrBit(TIM4_CR1, T4IF); //清除上溢中断标志位
    }
}

/*  -------------------------------------------------------------------------------------------------
    Function Name  : CMP3_INT
    Description    : CMP3：硬件比较器过流保护，关断输出，中断优先级最高
    Date           : 2020-04-10
    Parameter      : None
    ------------------------------------------------------------------------------------------------- */
void CMP3_INT(void) interrupt 12
{
    if (ReadBit(CMP_SR, CMP3IF))
    {
        mcFaultSource = FaultHardOVCurrent; // 硬件过流保护
        FaultProcess();
        mcState = mcFault;
        ClrBit(CMP_SR, CMP3IF);
    }
}

/* -------------------------------------------------------------------------------------------------
    Function Name  : LIN_IRQ
    Description    : LIN中断函数
    Date           : 2020-07-20
    Parameter      : None
------------------------------------------------------------------------------------------------- */
void LIN_IRQ(void) interrupt 14
{
   
}
